﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Model;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class RegisterUser : Form
    {
        OpenFileDialog openFile = new OpenFileDialog();
        public RegisterUser()
        {
            InitializeComponent();
            comboBoxGender.Text = "Male";            
            LoadComboBoxTypeManager();
            //IssuePermits();
        }

        private void LoadComboBoxTypeManager()
        {            
            DataTable listRole= RoleManager.LoadManagerRoles();
            for (int i = 0; i < listRole.Rows.Count; i++) 
            {
                comboBoxUserType.Items.Add(listRole.Rows[i]["NameRole"].ToString());
            }
        }

        //private void IssuePermits()
        //{
        //    //buttonSubmit.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        //}

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            Manager user = new Manager();
            Role role = new Role();
            if (ValidateFields.ValidatingFieldsNewUser())
            {
                if (UserManager.ValidateConfirmPassword(textBoxPassword.Text, textBoxConfirmPassword.Text))
                {                    
                    user.CIManager = textBoxCi.Text;
                    user.FirstName = textBoxFirstName.Text;
                    user.LastName = textBoxLastName.Text;
                    user.Address = textBoxAddress.Text;
                    user.CellPhone = int.Parse(textBoxCellPhone.Text);
                    user.Phone = int.Parse(textBoxPhone.Text);
                    user.DateInscription = DateTime.Today;
                    user.Email = textBoxFirstName.Text;
                    user.Gender = comboBoxGender.Text;                    
                    user.UserName = textBoxUserName.Text;
                    user.Password = textBoxPassword.Text;
                    user.StateManager = true;
                    user.ImageManager = openFile.SafeFileName;
                    
                    if (UserManager.InsertNewUser(user,comboBoxUserType.Text))
                    {
                        MessageBox.Show("Successful registration");
                        try
                        {
                            File.Copy(openFile.FileName, Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", openFile.SafeFileName)));

                        }
                        catch (Exception)
                        {   
                            
                        }
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("The user could not be registered");
                    }
                }
                else
                {
                    MessageBox.Show("The passwords you entered do not match.");
                }
            }
            else
            {
                MessageBox.Show("All fields are required");
            }
        }
        private void textBoxFirstName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxFirstName, ValidateFields.FirstNameValidation(textBoxFirstName.Text));
        }
        private void textBoxCi_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxCi,ValidateFields.CIValidation(textBoxCi.Text));
        }

        private void textBoxLastName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxLastName,ValidateFields.LastNameValidation(textBoxLastName.Text));
        }

        private void textBoxAddress_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxAddress, ValidateFields.AddressValidation(textBoxAddress.Text));
        }

        private void textBoxCellPhone_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxCellPhone,ValidateFields.CellPhoneValidation(textBoxCellPhone.Text));
        }

        private void textBoxPhone_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxPhone,ValidateFields.PhoneValidation(textBoxPhone.Text));
        }

        private void textBoxEmail_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxEmail,ValidateFields.EmailValidation(textBoxEmail.Text));
        }

        private void textBoxUserName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxUserName,ValidateFields.UserNameValidation(textBoxUserName.Text));
        }

        private void textBoxPassword_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxPassword,ValidateFields.PasswordValidation(textBoxPassword.Text));
        }

        private void textBoxConfirmPassword_Validating(object sender, CancelEventArgs e)
        {
            errorProviderRegisterUser.SetError(textBoxConfirmPassword,ValidateFields.ConfirmPasswordValidation(textBoxConfirmPassword.Text));
        }

        private void buttonPicture_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBoxPicture.ImageLocation = openFile.FileName;

            }
            else
            {
                pictureBoxPicture.Image = null;
            }
        }

        private void RegisterUser_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }
    }
}
